package com.shwread.android.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.shwread.android.qysw10000204.R;
import com.shwread.httpsdk.util.Util;

import java.util.List;

import logic.bean.ZoneInfo;


public class OrderSeatLocationDialog
        extends
        Dialog
{
	public interface onSelectSexListener
	{
		void onSelect(int location);
	}
	
	private Context     		context;
	private List<ZoneInfo>		listZones;
	private String				value;
	private DatePickerListener  listener;
	private ListView			lv;
	private Button				btnCancel;
	private int					posi		= 0;
	private MyAdapter			adapter 	= new MyAdapter();
	private View				currView;
	
	public OrderSeatLocationDialog(Context context, List<ZoneInfo> listZones, String value, DatePickerListener listener) {
		super(context, R.style.common_dialog);
		setCanceledOnTouchOutside(true);
		
		this.context = context;
		this.listZones = listZones;
		this.value = value;
		this.listener = listener;
		findViews();
		setListeners();
		
		getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		getWindow().setGravity(Gravity.BOTTOM);
		getWindow().setWindowAnimations(android.R.style.Animation_InputMethod);
	}
	
	private void findViews() {
		View view = LayoutInflater.from(context).inflate(R.layout.order_seat_location_dialog, null);
		lv = (ListView) view.findViewById(R.id.lv_order_seat_location);
		if(listZones.size()<=2) {
			lv.setVerticalScrollBarEnabled(false);
		} else {
			lv.setVerticalScrollBarEnabled(true);
		}
		btnCancel = (Button) view.findViewById(R.id.btn_order_seat_location_cancel);
		setContentView(view);
		for (int i = 0; i < listZones.size(); i++) {
			if(Util.isEmpty(value)) {
				posi = 0;
			}else {
				if(value.equals(listZones.get(i).getZoneName())) {
					posi = i;
					break;
				}
			}
		}
		lv.setAdapter(adapter);
//		Utils.setListViewHeightBasedOnChildren(lv, adapter);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				posi=arg2;
				adapter.notifyDataSetChanged();
				listener.onSelect(listZones.get(arg2).getZoneName());
				dismiss();
			}
		});
	}
	
	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listZones.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			View view;
			if(arg1==null) {
				view = View.inflate(context, R.layout.order_seat_location_dialog_list_item, null);
			} else {
				view = arg1;
			}
			Button btn = (Button) view.findViewById(R.id.btn_order_seat_location);
			btn.setText(listZones.get(arg0).getZoneName());
			if(arg0==posi) {
				btn.setBackgroundResource(R.drawable.order_seat_location_button_pressed);
				btn.setTextColor(Color.parseColor("#FFFFFF"));
			}else {
				btn.setBackgroundResource(R.drawable.order_seat_location_button_normal);
				btn.setTextColor(Color.parseColor("#F26C37"));
			}
			if(currView==null && arg0==posi) {
				currView=view;
			}
			view.setOnTouchListener(new OnTouchListener() {
				
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					posi = arg0;
					adapter.notifyDataSetChanged();
					return false;
				}
			});
			return view;
		}
		
	}
	
	private void setListeners() {
		ClickListener l = new ClickListener();
		btnCancel.setOnClickListener(l);
	}
	
	private class ClickListener
	        implements
	        View.OnClickListener
	{
		
		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
				case R.id.btn_order_seat_location_cancel:
					dismiss();
					break;
			}
		}
	}
	
}
